package com.atguigu.tingshu.search.service.impl;

import com.atguigu.tingshu.album.client.AlbumInfoFeignClient;
import com.atguigu.tingshu.album.client.CategoryFeignClient;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.album.BaseCategoryView;
import com.atguigu.tingshu.search.service.ItemService;
import com.atguigu.tingshu.user.client.UserInfoFeignClient;
import com.atguigu.tingshu.vo.album.AlbumStatVo;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

@Slf4j
@Service
@SuppressWarnings({"all"})
public class ItemServiceImpl implements ItemService {

    @Autowired
    private AlbumInfoFeignClient albumInfoFeignClient;

    @Autowired
    private CategoryFeignClient categoryFeignClient;

    @Autowired
    private UserInfoFeignClient userInfoFeignClient;

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;



    /*
    result.put("albumInfo", albumInfo);			获取专辑信息
    result.put("albumStatVo", albumStatVo);		获取专辑统计信息
    result.put("baseCategoryView", baseCategoryView);	获取分类信息
    result.put("announcer", userInfoVo);	获取主播信息
    */

    /**
     * 获取专辑详情页面
     *
     * @param albumId
     * @return
     */
    @Override
    public Map<String, Object> getAlbumDetails(Long albumId) {
        //todo 创建存放信息的map对象
        Map<String, Object> map = new HashMap<>();
        //使用complatablelFuture将四个远程调用进行异步操作


        CompletableFuture<AlbumInfo> albumInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
            //todo 1.远程调用专辑信息
            Result<AlbumInfo> albumInfoResult = albumInfoFeignClient.getAlbumInfo(albumId);
            Assert.notNull(albumInfoResult.getData(), "专辑信息不存在");
            //获取专辑信息
            AlbumInfo albumInfo = albumInfoResult.getData();
            Assert.notNull(albumInfo, "专辑信息不存在");
            map.put("albumInfo", albumInfo);
            return albumInfo;
        },threadPoolExecutor);

        CompletableFuture<Void> statCompletableFuture = albumInfoCompletableFuture.runAsync(()-> {
            //todo 2.远程调用statvo
            Result<AlbumStatVo> albumStatVoResult = albumInfoFeignClient.getAlbumStat(albumId);
            Assert.notNull(albumStatVoResult.getData(), "专辑统计信息不存在");
            //获取专辑统计信息
            AlbumStatVo albumStatVo = albumStatVoResult.getData();
            Assert.notNull(albumStatVo, "专辑统计信息不存在");
            map.put("albumStatVo", albumStatVo);
        },threadPoolExecutor);


        CompletableFuture<Void> categoryCompletableFuture = albumInfoCompletableFuture.thenAcceptAsync(albumInfo -> {
            //todo 3.远程调用分类
            Result<BaseCategoryView> categoryViewResult = categoryFeignClient.getCategoryView(albumInfo.getCategory3Id());
            Assert.notNull(categoryViewResult.getData(), "分类信息不存在");
            //获取分类信息
            BaseCategoryView baseCategoryView = categoryViewResult.getData();
            Assert.notNull(baseCategoryView, "分类信息不存在");
            map.put("baseCategoryView", baseCategoryView);
        }, threadPoolExecutor);


        CompletableFuture<Void> userInfoCompletableFuture = albumInfoCompletableFuture.thenAcceptAsync(albumInfo -> {
            //todo 4.远程调用用户信息
            Result<UserInfoVo> userInfoVoResult = userInfoFeignClient.getUserInfoVoByUserId(albumInfo.getUserId());
            Assert.notNull(userInfoVoResult.getData(), "用户信息查询失败");
            //获取用户信息
            UserInfoVo userInfoVo = userInfoVoResult.getData();
            Assert.notNull(userInfoVo, "用户信息查询失败");
            map.put("announcer", userInfoVo);
        }, threadPoolExecutor);

        CompletableFuture.allOf(
                albumInfoCompletableFuture,
                statCompletableFuture,
                categoryCompletableFuture,
                userInfoCompletableFuture
        ).join();

        return map;
    }
}
